package com.tf_cut.backend.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.tf_cut.backend.agent.vo.AgentAndAccountVo;
import com.tf_cut.backend.agent.vo.AgentSellerRelationVo;
import com.tf_cut.backend.api.pojo.ApiPaidRes;
import com.tf_cut.backend.api.pojo.SinglePaid;
import com.tf_cut.backend.api.service.ApiService;
import com.tf_cut.backend.mapper.*;
import com.tf_cut.backend.seller.pojo.ProvideDetail;
import com.tf_cut.backend.seller.pojo.SellerOrder;
import com.tf_cut.backend.seller.service.SellerAccountDetailService;
import com.tf_cut.backend.seller.vo.SellerAccountDetailVo;
import com.tf_cut.backend.seller.vo.SellerAndAccountVo;
import com.tf_cut.backend.service.vo.ServiceRefSellerResVo;
import com.tf_cut.backend.util.OrderPaymentQueue;
import com.tf_cut.backend.util.SerialNoUtil;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ApiServiceImpl implements ApiService {

    @Autowired
    private SellerAndAccountMapper sellerAndAccountMapper;

    @Autowired
    private ApiMapper apiMapper;
    
	@Autowired
	private SellerAccountDetailService sellerAccountDetailService;

    @Autowired
    private ServiceRefSellerMapper serviceRefSellerMapper;

    @Autowired
    private AgentSellerRelationMapper agentSellerRelationMapper;

    @Autowired
    private AgentAndAccountMapper agentAndAccountMapper;

    @Autowired
    private SellerOrderMapper sellerOrderMapper;
    
    @Autowired
    private SerialNoUtil serialNoUtil;

    @Override
    public String getSellerInfo(String appKey) {
        SellerAndAccountVo vo = new SellerAndAccountVo();
        vo.setAppKey(appKey);
        List<SellerAndAccountVo> list = sellerAndAccountMapper.findByCondition(vo);
        return CollectionUtils.isNotEmpty(list) ? JSON.toJSONString(list.get(0)) : "";
    }

    @Override
    public String singlePaid(SinglePaid singlePaid,String platform,String merchantNumber) {
        SellerAndAccountVo vo = new SellerAndAccountVo();
        vo.setSellerId(singlePaid.getSellerId());
        List<SellerAndAccountVo> list = sellerAndAccountMapper.findByCondition(vo);
        List<ServiceRefSellerResVo> serviceList = serviceRefSellerMapper.findServiceInfo(singlePaid.getSellerId());

        if(CollectionUtils.isEmpty(serviceList)){
            return "noServiceInfo";
        }
        AgentSellerRelationVo avo = new AgentSellerRelationVo();
        avo.setSellerId(singlePaid.getSellerId());
        List<AgentSellerRelationVo> agentList = agentSellerRelationMapper.findListRelationBySeller(avo);

        if(CollectionUtils.isEmpty(agentList)){
            return "noAgentInfo";
        }

        AgentSellerRelationVo agent = agentList.get(0);
        AgentAndAccountVo agVo = new AgentAndAccountVo();
        agVo.setAgentId(agent.getAgentId());
        List<AgentAndAccountVo> agList = agentAndAccountMapper.findListAgent(agVo);
        

        SellerAndAccountVo seller = list.get(0);
        ServiceRefSellerResVo service = serviceList.get(0);
        AgentAndAccountVo agentA = agList.get(0);

        //获取ID
        String orderId =serialNoUtil.genSerialNo("F",platform,merchantNumber);
        String detailId =serialNoUtil.genSerialNo("Q",platform,merchantNumber);
        String serialNo = serialNoUtil.genSerialNo("Q",platform,merchantNumber);

        ProvideDetail detail = new ProvideDetail();
        detail.setId(detailId);
        detail.setFbalanceSerial(BigDecimal.valueOf(System.currentTimeMillis()));
        detail.setOrderId(orderId);
        detail.setTradeSerial(BigDecimal.valueOf(System.currentTimeMillis()));
        detail.setAskTime(new Timestamp(System.currentTimeMillis()));
        detail.setCardCode(singlePaid.getIdentityCardNum());
        detail.setPayeeName(singlePaid.getName());
        detail.setPayeeAccount(singlePaid.getBankCardNum());
        detail.setPayAmount(BigDecimal.valueOf(Double.parseDouble(singlePaid.getAmount())));
        detail.setPayeeAccountBankName(singlePaid.getBankName());
        detail.setPayeeMobile(singlePaid.getMobile());
        detail.setPayeeRemark(singlePaid.getRemark());
        detail.setStatus(1);
        detail.setPaymentStatus("O");

        //approve_status = 1 and order_type = 1
        SellerOrder sellerOrder = new SellerOrder();
        sellerOrder.setOrderId(orderId);
        sellerOrder.setSellerId(seller.getSellerId());
        sellerOrder.setSellerName(seller.getSellerName());
        sellerOrder.setServiceId(service.getServiceId());
        sellerOrder.setServiceName(service.getServiceName());
        sellerOrder.setApplyTime(new Timestamp(System.currentTimeMillis()));
        sellerOrder.setRealAmount(detail.getPayAmount());
        BigDecimal serviceFee = detail.getPayAmount().multiply(agent.getServerAmountPoint()).divide(new BigDecimal(100));
        BigDecimal provideAmount = detail.getPayAmount().add(serviceFee);
        sellerOrder.setProvideAmount(provideAmount);
        sellerOrder.setBaseAmountPoint(agentA.getBaseAmountPoint());
        sellerOrder.setServerAmountPoint(agent.getServerAmountPoint());
        sellerOrder.setOverAmountPoint(sellerOrder.getServerAmountPoint().subtract(sellerOrder.getBaseAmountPoint()));
        sellerOrder.setStepAmountPoint(agentA.getStepAmountPoint());
        sellerOrder.setOrderType(0);
        sellerOrder.setApproveStatus(1);
        singlePaid.setOrderId(orderId);
        singlePaid.setDetailId(detailId);
        
        //扣服务费
        SellerAccountDetailVo sellerAccountDetailVo = new SellerAccountDetailVo();
        Timestamp now = Timestamp.valueOf(LocalDateTime.now());
        sellerAccountDetailVo.setTradeId(serialNo);
        sellerAccountDetailVo.setSellerAccountId(seller.getSellerAccountId());
        sellerAccountDetailVo.setTradeSerial(orderId);
        sellerAccountDetailVo.setOrderSource("00");
        sellerAccountDetailVo.setOperType(-1);
        sellerAccountDetailVo.setPayTime(now);
        sellerAccountDetailVo.setPaySubject("0001");
        sellerAccountDetailVo.setPayType(1);
        sellerAccountDetailVo.setTradeMoney(BigDecimal.valueOf(Double.parseDouble(singlePaid.getAmount())));
        sellerAccountDetailVo.setTradeComment("发放费用-api");
        sellerAccountDetailVo.setIsTotal("N");
        sellerAccountDetailVo.setTotalTime(now);
        sellerAccountDetailVo.setCpmTime(now);
        sellerAccountDetailVo.setSellerId(seller.getSellerId());
        sellerAccountDetailVo.setServiceId(service.getServiceId());
        sellerAccountDetailService.save(sellerAccountDetailVo);

        sellerOrderMapper.insOrder(sellerOrder);
        sellerOrderMapper.insOrderDetail(detail);
        apiMapper.insRecord(singlePaid);

        OrderPaymentQueue.set(orderId);
        return detailId;
    }

    @Override
    public List<ApiPaidRes> qryRecord(List<String> noList) {
        return apiMapper.qryRecord(noList);
    }


}
